<?php
require_once("Exercises/Model/Exercise.php");
require_once("login/src/Model/LoginValidation.php");
require_once("Database/Model/DatabaseConnection.php");

class ExerciseDAL  
{
    /**
    *@var LoginValidation
    **/
    private $loginValidation;

	public function __construct()
	{
        $this->loginValidation = new LoginValidation();
	}

    /**
    *@param int
    *@return exercise array 
    **/
	public function getExerciseList($exerciseID = null)
	{
        $userID =  $this->loginValidation->getLoggedInUserID();
        if($exerciseID  != null)
        {
            $sql = "SELECT `ExerciseID`, MuscleGroupID, Name, Description,YoutubeLink, UserInfoID 
                    FROM exercise WHERE UserInfoID = $userID AND ExerciseID = $exerciseID";

            $exercise = $this->ExerciseList($sql);      
            if(isset($exercise[0]))
            {
                return $exercise[0];
            }
            else
            {
            
                return;
            }
        }
        else
        {
		    $sql = "SELECT `ExerciseID`, MuscleGroupID, Name, Description,YoutubeLink, UserInfoID 
                    FROM exercise WHERE UserInfoID = $userID ORDER BY MuscleGroupID";
            return $this->ExerciseList($sql);
        }
		
	}

    /**
    *@param string sql
    *@throws new exceptions
    *@return exercises array
    **/
	public function ExerciseList($sql)
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

		$stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception($sql);
        }

        
        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($exerciseID, $muscleGroupID, $name, $description, $youtubeLink, $userID);
        if ($result == FALSE)
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $exerciseList = array();
 
        while ($stmt->fetch()) 
        {
            $musclegroupName = $this->getMuscleGroupNameOnID($muscleGroupID);
            
            $exerciseList[] = Exercise::createNewExerciseFromDatabase($musclegroupName,
                                                                      $muscleGroupID,
            													      $name, 
            													      $description, 
                                                                      $youtubeLink,
                                                                      $userID,
                                                                      $exerciseID);
        }
        $mysqli->close();
        return $exerciseList;
	}



    /**
    *@param int
    *@throws new exceptions
    *@return string
    **/
    public function getMuscleGroupNameOnID($muscleGroupID)
    {
        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());
        
        $sql = "SELECT  `Name` FROM mousclegroups WHERE `MuscleGroupID` = ?";
        
        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->bind_param("i", $muscleGroupID);
        if ($result == FALSE)
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }
        
        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($name);
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }
        $theName = "";
        while ($stmt->fetch()) 
        {
            $theName = $name;
        }   

        $mysqli->close();
        return $name;
    }


    /**
    *@param Exercise
    *@throws new exceptions
    **/
	public function saveNewExercise(Exercise $exercises)
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

        $mgID = $exercises->getMusclegroupID();
        $name = $exercises->getExerciseName();
        $description = $exercises->getExerciseDescription();
        $youtubeLink =  $exercises->getYoutubeLink();

	    $sql = "INSERT INTO exercise 
                            (
                                UserInfoID, 
                            	MuscleGroupID, 
                            	Name, 
                            	Description,
                                YoutubeLink 
                            ) 
                            VALUES(?, ?, ?, ?, ?)";

        $statement = $mysqli->prepare($sql);
        if ($statement === FALSE) 
        {
            throw new Exception("prepare of $sql failed " . $this->mysqli->error); 
        }

        $exerciseID = $exercises->getExerciseID();
        $userID = $this->loginValidation->getLoggedInUserID();
        $muscleGroupID = $exercises->getMusclegroupID();
        $exerciseName = $exercises->getExerciseName(); 
        $exerciseDescription = $exercises->getExerciseDescription();
        $youtubeLink = $exercises->getYoutubeLink();

        if ($statement->bind_param("iisss", $userID,
                                            $muscleGroupID, 
                                            $exerciseName, 
                                            $exerciseDescription,
                                            $youtubeLink) === FALSE) 
        {
            throw new Exception("bind_param of $sql failed " . $statement->error);
        }

        if ($statement->execute() === FALSE) 
        {
            throw new Exception("execute of $sql failed " . $statement->error);
        }
        
		$mysqli->close();
	}




    /**
    *@param Exercise
    *@param int
    *@throws new exceptions
    **/
    public function EditExercise(Exercise $exercises, $exerciseID)
    {
        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

        $mgID = $exercises->getMusclegroupID();
        $name = $exercises->getExerciseName();
        $description = $exercises->getExerciseDescription();
        $youtubeLink =  $exercises->getYoutubeLink();

   
        $sql = "UPDATE `exercise` 
                    SET MuscleGroupID = ?,  
                    Name = ?,
                    Description = ?,
                    YoutubeLink = ? 
                    WHERE ExerciseID = ?";

        $statement = $mysqli->prepare($sql);
        if ($statement === FALSE) 
        {
            throw new Exception("prepare of $sql failed " . $this->mysqli->error);
        }

        if ($statement->bind_param("isssi", $mgID,
                                            $name,
                                            $description,
                                            $youtubeLink,
                                            $exerciseID) === FALSE) 
        {
            throw new Exception("bind_param of $sql failed " . $statement->error);
        }

        if ($statement->execute() === FALSE) 
        {
            throw new Exception("execute of $sql failed " . $statement->error);
        }
        
        $mysqli->close();
    }










    /**
    *@param int
    *@throws new exceptions
    **/
    public function DeleteExercise($exerciseID)
    {
        $sql = "DELETE FROM exercise WHERE `ExerciseID` = $exerciseID"; 

        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $mysqli->close();

    }
}